/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditProjectService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditCaseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditNotificationDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.audit.dto.AuditProjectDTO;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对案件信息录入
 * 
 * @author EX-LUORONG001
 * 
 */
@Controller
@RequestMapping("/auditProject/")
public class AuditProjectController {

	@BizServicesProxy(beanName = "auditProjectService")
	private AuditProjectService auditProjectService;

	@BizServicesProxy(beanName = "integrityMedicalService")
	private IntegrityMedicalService integrityMedicalService;

	/**
	 * 根据案件编号编码加载对应的案件信息
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("queryAuditCase.do")
	@ResponseBody
	public Map<String, Object> queryAuditCase(@ModelAttribute
	AuditCaseDTO caseDTO, HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> caseDto = this.auditProjectService.getCase(caseDTO);
		result.put("result", caseDto);
		return result;
	}

	/**
	 * 根据案件编号加载对应的稽核项目
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("queryAuditProjectList.do")
	@ResponseBody
	public Map<String, Object> queryAuditProjectList(@ModelAttribute
	AuditProjectDTO caseDTO, HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> caseDto = this.auditProjectService
				.getAuditProjectList(caseDTO);
		result.put("result", caseDto);
		return result;
	}

	/**
	 * 根据案件编号修改对应稽核项目 及添加稽核结论到意见表
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateAuditProject.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateAuditProject(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditCaseDTO caseDTO = JSONObject.toJavaObject(transferJson,
					AuditCaseDTO.class);
			caseDTO.setOpinion("0");
			caseDTO.setUpdatedBy(user.getUsername());

			AuditOpinionDTO opinionDTO = new AuditOpinionDTO();
			opinionDTO.setDealUserName(user.getUsername());
			opinionDTO.setOpinionType(AuditConstants.opinionType_11);
			opinionDTO.setOpinion(caseDTO.getOpinion());

			result = this.auditProjectService.updateCase(caseDTO, opinionDTO,
					user, transferJson, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据案件编号修改对应稽核项目 及添加稽核结论到意见表（批量提交）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateBatchAuditProject.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> updateBatchAuditProject(
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			// 获取批量稽核的案件编号
			String caseCode[] = request.getParameterValues("caseCodes");

			result = this.auditProjectService.updateBatchCase(user, caseCode,
					result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据案件编号获取稽核项目不合理费用总额
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("getCaseDeductionAmmount.do")
	@ResponseBody
	public Map<String, Object> getCaseDeductionAmmount(@ModelAttribute
	AuditProjectDTO caseDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		String ammount = this.auditProjectService.getDeductionAmmount(caseDTO);
		result.put("ammount", ammount);
		return result;
	}

	/**
	 * 根据案件编号获取医疗机构信息
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("getAuditNotificationInfo.do")
	@ResponseBody
	public Map<String, Object> getAuditNotificationInfo(@ModelAttribute
	AuditNotificationDTO notificationDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		result = this.auditProjectService.getAuditNotification(notificationDTO,
				result);
		return result;
	}

}
